﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Diagnostics;
using Knowledgecast.Twitter.WPF.App.Properties;
using System.Collections;

namespace Knowledgecast.Twitter.WPF.App
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1
    {
        public Window1()
        {
            InitializeComponent();            
        }

        public string Username { get; set; }
        public string Password { get; set; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Username = Settings.Default.TwitterUsername;
            Password = Settings.Default.TwitterPassword;

            if (String.IsNullOrEmpty(Username) || String.IsNullOrEmpty(Password))
            {
                btnLogout.Visibility = Visibility.Collapsed;
                gridLogin.Visibility = Visibility.Visible;
            }
            else
            {
                LoadStartupPanels();
            }
        }

        void LoadStartupPanels()
        {
            btnLogout.Visibility = Visibility.Visible;
            ctrlStatusUpdate.Twitter = new TwitterApi(Username, Password);

            if (Settings.Default.AllFriends == false && Settings.Default.Replies == false && (Settings.Default.SearchQueries == null || Settings.Default.SearchQueries.Count == 0))
            {
                LoadDefaultStartupPanels();
            }
            else
            {
                if (Settings.Default.AllFriends)
                {
                    btnAllFriends.Visibility = Visibility.Collapsed;
                    AddAllFriends();
                }

                if (Settings.Default.Replies)
                {
                    btnReplies.Visibility = Visibility.Collapsed;
                    AddReplies();
                }

                if (Settings.Default.SearchQueries != null && Settings.Default.SearchQueries.Count > 0)
                {
                    foreach (string query in Settings.Default.SearchQueries)
                    {
                        AddSearchTimeline(query);
                    }
                }
            }
        }

        void LoadDefaultStartupPanels()
        {
            Settings.Default.AllFriends = true;
            Settings.Default.Save();
            LoadStartupPanels();
        }

        private void AddReplies()
        {
            if (btnAllFriends.Visibility == Visibility.Collapsed)
            {
                AddFriendsTimeline("Replies", 1, TimelineMode.Replies);
            }
            else
            {
                AddFriendsTimeline("Replies", 0, TimelineMode.Replies);
            }
        }

        void AddAllFriends()
        {
            AddFriendsTimeline("All Friends", 0, TimelineMode.Friends);
        }

        private void AddFriendsTimeline(string title, int index, TimelineMode mode)
        {
            var ft = new FriendsTimeline();
            ft.CloseRequested += TimelineCloseRequested;
            ft.Twitter = new TwitterApi(Username, Password);
            ft.Mode = mode;
            ft.PanelTitle = title;

            if (index != -1)
            {
                Main.Children.Insert(index, ft);
            }
            else
            {
                Main.Children.Add(ft);
            }
        }

        void TimelineCloseRequested(ITimelinePanel panel)
        {
            if (panel.PanelTitle == "All Friends")
            {
                btnAllFriends.Visibility = Visibility.Visible;
                Settings.Default.AllFriends = false;
                Settings.Default.Save();
            }
            else if(panel.PanelTitle == "Replies")
            {
                btnReplies.Visibility = Visibility.Visible;
                Settings.Default.Replies = false;
                Settings.Default.Save();
            }
            else if (panel is SearchTimeline)
            {
                Settings.Default.SearchQueries.Remove((panel as SearchTimeline).Query);
                Settings.Default.Save();
            }

            Main.Children.Remove(panel as UIElement);
        }

        private void AddSearchTimeline(string query)
        {
            var st = new SearchTimeline();
            st.CloseRequested += TimelineCloseRequested;
            st.PanelTitle = "Search: " + query;
            st.Twitter = new TwitterApi(Username, Password);
            st.Query = query;

            Main.Children.Add(st);
        }        

        private void btnTweet_Click(object sender, RoutedEventArgs e)
        {
            if (gridTweet.Visibility == Visibility.Visible)
            {
                gridTweet.Visibility = Visibility.Collapsed;
            }
            else
            {
                gridTweet.Visibility = Visibility.Visible;
                ctrlStatusUpdate.Focus();
                ctrlStatusUpdate.SetFocus();
            }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            var search = new NewSearch {Owner = this};
            if (search.ShowDialog().Value)
            {
                if (Settings.Default.SearchQueries == null)
                {
                    Settings.Default.SearchQueries = new ArrayList();
                }

                Settings.Default.SearchQueries.Add(search.Query);
                Settings.Default.Save();
                AddSearchTimeline(search.Query);
            }            
        }

        private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            InvalidateMeasure();
        }

        private void btnAllFriends_Click(object sender, RoutedEventArgs e)
        {
            btnAllFriends.Visibility = Visibility.Collapsed;
            Settings.Default.AllFriends = true;
            Settings.Default.Save();
            AddAllFriends();
        }

        private void btnReplies_Click(object sender, RoutedEventArgs e)
        {
            btnReplies.Visibility = Visibility.Collapsed;
            Settings.Default.Replies = true;
            Settings.Default.Save();
            AddReplies();
        }

        private void Reply_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var s = e.Parameter as Status;
            if (s != null)
            {
                Trace.WriteLine("Reply " + s.Text);
                gridTweet.Visibility = Visibility.Visible;                
                ctrlStatusUpdate.Setup("@" + s.User.ScreenName + " ", s);
                ctrlStatusUpdate.Focus();
                ctrlStatusUpdate.SetFocus();
            }
        }

        private void Retweet_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var s = e.Parameter as Status;
            if (s != null)
            {
                Trace.WriteLine("Retweet " + s.Text);
                gridTweet.Visibility = Visibility.Visible;
                ctrlStatusUpdate.Setup("RT " + "@" + s.User.ScreenName + ": " + s.Text, null);
                ctrlStatusUpdate.Focus();
                ctrlStatusUpdate.SetFocus();
            }
        }

        private void DirectMessage_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var s = e.Parameter as Status;
            if (s != null)
            {
                Trace.WriteLine("Direct Message " + s.Text);
                gridTweet.Visibility = Visibility.Visible;
                ctrlStatusUpdate.Setup("D " + s.User.ScreenName + " ", null);
                ctrlStatusUpdate.Focus();
                ctrlStatusUpdate.SetFocus();
            }
        }

        private void OtherActions_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            var s = e.Parameter as Status;
            if (s != null)
            {
                Trace.WriteLine("Other Actions " + s.Text);
            }
        }

        private void LoginInformation(string username, string password, bool persist)
        {
            Username = username;
            Password = password;

            if (persist)
            {
                Settings.Default.TwitterUsername = username;
                Settings.Default.TwitterPassword = password;
                Settings.Default.Save();
            }

            gridLogin.Visibility = Visibility.Collapsed;
            LoadStartupPanels();
        }

        private void btnLogout_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default.TwitterUsername = null;
            Settings.Default.TwitterPassword = null;
            Settings.Default.Save();

            Main.Children.Clear();

            btnLogout.Visibility = Visibility.Collapsed;
            gridLogin.Visibility = Visibility.Visible;
        }

        private void ShowUrl_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Trace.WriteLine("ShowUrl: " + e.Parameter);
            Process.Start(e.Parameter as string);            
        }
    }
}
